﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Yunchang;

namespace AutoChess
{
    public class MovePackage
    {
        public CommonAnimCurve cure;
        public Action endCall;
        public MovePackage(CommonAnimCurve cure, Action endCall)
        {
            this.cure = cure;
            this.endCall = endCall;
        }
    }
    public class ShipMoveState : Yunchang.FsmStateBase<ShipState>
    {
        private CommonAnimCurve cure;
        private Action endCall;

        private float eplose;
        private ShipBehaviourCtr ctr;
        private float lastEplose;
        public ShipMoveState(ShipState stateType, ShipBehaviourCtr controller) : base(stateType, controller)
        {
            ctr = controller;
        }

        public override void Enter()
        {
            base.Enter();
            cure = ctr.package.cure;
            endCall = ctr.package.endCall;
            eplose = 0;
            lastEplose = 0;
            CommonEffectManager.Instance.PlayEffectImmediate(CommonEffectResId.AC_150_zizouqi_chuanshenshuilang, ctr.render.transform);


        }

        public override void Update()
        {
            base.Update();
            eplose += Time.unscaledDeltaTime;

            if (eplose > cure.AnimRealTime)
            {
                if (endCall != null)
                {
                    endCall();
                    endCall = null;
                }
                ctr.SwitchState(ShipState.Idel);
                return;
            }
            else
            {

                var lastPos = cure.EvaluatePosition(lastEplose / cure.AnimRealTime);
                var curPos = cure.EvaluatePosition(eplose / cure.AnimRealTime);
                ctr.render.transform.position = curPos;

                var angles = (lastPos - curPos).normalized;
                ctr.render.transform.forward = angles;

                lastEplose = eplose;
            }
        }
    }
}
